package cn.tedu._05mvcboot03.common.exception;

import cn.tedu._05mvcboot03.common.response.JsonResult;
import cn.tedu._05mvcboot03.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult MethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        log.error("DefaultHandlerExceptionResolver="+ex.getFieldError().getDefaultMessage());
        return new JsonResult(StatusCode.VALIDATED_ERROR,ex.getFieldError().getDefaultMessage());
    }
    @ExceptionHandler
    public JsonResult ConstraintViolationException(ConstraintViolationException ex) {
        log.error("ConstraintViolationException="+ex.getMessage().split(":")[1].trim());
        return new JsonResult(StatusCode.VALIDATED_ERROR,ex.getMessage().split(":")[1].trim());
    }

}
